<?php

/**
 * @content: 接收上传过来的标本数据
 * @Author:  Groube
 * @Date:    2017-03-06 17:00:00
 * @version  $Id$
 */
class BiaobendatauploadController extends Yaf_Controller_Abstract {

    /**
     * @Title  禁用模板功能
     * @Author Groube
     * @Param  /
     * @Return /
     * @Date 2017/03/08 14:30
     */
    public function init() {
        $yiyuan_id = isset($_REQUEST['yiyuan_id']) ? $_REQUEST['yiyuan_id'] : "";

        if($yiyuan_id === "" ){
            Utility::apiJsonError(20028, '请输入医院ID');
        }elseif(!ctype_digit("$yiyuan_id")){
            Utility::apiJsonError(20027, '医院ID必须是整型');
        }

        Yaf_Dispatcher::getInstance()->disableView();
    }


    /**
     * @Title  入口方法
     * @Author Groube
     * @Param  /
     * @Return /
     * @Date 2017/03/08 14:20
     */
    function indexAction(){
        $request = $this->getRequest();
        $data = $request->getPost('data','');
        $user_department_id = $request->getPost('user_department_id','');
        $str_yiyuan_id = $request->getPost('yiyuan_id',0);
        $yiyuan_id = (int)$str_yiyuan_id;

        if(Yaconf::get("config$yiyuan_id.fenbiao_state") == 'true'){
            //分表机制开启
            $zhuyuan_fuzhujiancha = "zhuyuan_fuzhujiancha_".$user_department_id;
        }else{
            $zhuyuan_fuzhujiancha = "zhuyuan_fuzhujiancha";
        }

        /*是否开启压缩 默认开启*/
        $compression_http_data = $request->getPost('compression_http_data','off');

        //解压缩数据
        $upload_data = json_decode(Utility::uncompress($data,$compression_http_data),true);

        if(empty($data)){
            Utility::apiJsonError(40002,'(Method:接收上传过来的标本数据)数据不能为空!');
        }else{
            if(!is_array($upload_data) || empty($upload_data)){
                Utility::apiJsonError(40003,'(Method:接收上传过来的标本数据)传入的json格式的字符串有误!');
            }else{
                $this->checkParams($upload_data);
            }
        }

        $db = Kohana_Database::instance();
        $db->begin();
        $status = '1';
        try{
            foreach($upload_data as $one){
                //循环遍历对表$zhuyuan_fuzhujiancha数据更新
                $id = trim($one['id']);
                $biaoben_tiaoma = trim($one['biaoben_tiaoma']);
                $jiancha_hedui_zhuangtai = trim($one['jiancha_hedui_zhuangtai']);
                $hedui_hushi_mingcheng = trim($one['hedui_hushi_mingcheng']);
                $hedui_hushi_id = trim($one['hedui_hushi_id']);
                $hedui_time = trim($one['hedui_time']);
                $modify_time = date('Y-m-d H:i:s',time());

                DB::update($zhuyuan_fuzhujiancha)
                    ->set(
                        array(
                            'jiancha_hedui_zhuangtai' => $jiancha_hedui_zhuangtai,
                            'jiancha_hedui_hushi_name' => $hedui_hushi_mingcheng,
                            'jiancha_hedui_hushi_id' => $hedui_hushi_id,
                            'jiancha_hedui_time' => $hedui_time,
                            'modify_time' => $modify_time
                        )
                    )
                    ->where('id','=',$id)
                    ->where('biaoben_tiaoma','=',$biaoben_tiaoma)
                    ->where('yiyuan_id','=',$yiyuan_id)
                    ->execute();
            }

            $db->commit();
        }catch (Kohana_Exception $e){
            $db->rollback();
            $status = '0';
        }

        $statr = array('state' => $status);
        $statr['error'] =  200;
        $statr['msg'] = "";
        Utility::apiJsonResultExtends($statr);
    }


    /**
     * @Title  校验json字符串的参数是否正确
     * @Author Groube
     * @Param  array $upload_data json字符串
     * @Return /
     * @Date 2017/03/08 14:40
     */
    function checkParams($upload_data){
        foreach($upload_data as $item){
            if(!isset($item['id'])){
                Utility::apiJsonError(40004,'(Method:接收上传过来的标本数据)传入的json格式的字符串中缺少字段:[id]!');
            }

            if(!isset($item['biaoben_tiaoma'])){
                Utility::apiJsonError(40005,'(Method:接收上传过来的标本数据)传入的json格式的字符串中缺少字段:[biaoben_tiaoma]!');
            }

            if(!isset($item['jiancha_hedui_zhuangtai'])){
                Utility::apiJsonError(40006,'(Method:接收上传过来的标本数据)传入的json格式的字符串中缺少字段:[jiancha_hedui_zhuangtai]!');
            }

            if(!isset($item['hedui_hushi_mingcheng'])){
                Utility::apiJsonError(40007,'(Method:接收上传过来的标本数据)传入的json格式的字符串中缺少字段:[hedui_hushi_mingcheng]!');
            }

            if(!isset($item['hedui_hushi_id'])){
                Utility::apiJsonError(40008,'(Method:接收上传过来的标本数据)传入的json格式的字符串中缺少字段:[hedui_hushi_id]!');
            }

            if(!isset($item['hedui_time'])){
                Utility::apiJsonError(40009,'(Method:接收上传过来的标本数据)传入的json格式的字符串中缺少字段:[hedui_time]!');
            }else{
                //对传入的jiancha_hedui_time字符串做校验
                $hedui_time = $item['hedui_time'];
                $is_date = $this->checkDateIsValid($hedui_time);
                if(!$is_date){
                    Utility::apiJsonError(40010,'(Method:接收上传过来的标本数据)传入的json格式的字符串中字段:[hedui_time]格式有误!');
                }
            }
        }
    }


    /**
     * 校验日期格式是否正确
     *
     * @param string $date 日期
     * @param string $formats 需要检验的格式数组
     * @return boolean
     */
    function checkDateIsValid($date,$formats = array("Y-m-d H:i:s","Y/m/d H:i:s")) {
        $unixTime = strtotime($date);
        if(!$unixTime){
            //strtotime转换不对,日期格式不对
            return false;
        }

        //校验日期的有效性,只要满足其中一个格式就OK
        foreach($formats as $format){
            if(date($format, $unixTime) == $date){
                return true;
            }
        }

        return false;
    }
}